<?php

/**
 * This is the model class for table "{{ord_msg}}".
 *
 * The followings are the available columns in table '{{ord_msg}}':
 * @property integer $zom_id
 * @property string $zo_id
 * @property integer $zom_type
 * @property integer $zm_id
 * @property integer $zom_status
 * @property integer $inputtime
 * 张梦林
 */
class OrdMsg extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return OrdMsg the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{ord_msg}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zo_id', 'required'),
			array('zom_type, zm_id, zom_status, inputtime', 'numerical', 'integerOnly'=>true),
			array('zo_id', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zom_id, zo_id, zom_type, zm_id, zom_status, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'order'=>array(self::BELONGS_TO, 'Order', 'zo_id'),	//订单
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zom_id' => 'ID',
			'zo_id' => '订单号',
			'zom_type' => '消息类型',
			'zm_id' => '接收会员',
			'zom_status' => '状态',
			'inputtime' => '发送时间',
		);
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
	
		$criteria=new CDbCriteria;
	
		$criteria->compare('zom_id',$this->zom_id);
		$criteria->compare('zo_id',$this->zo_id,true);
		$criteria->compare('zom_type',$this->zom_type);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zom_status',$this->zom_status);
		$criteria->compare('inputtime',$this->inputtime);
	
		$criteria->order = 'inputtime DESC';
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}
	
	/**
	 * 载入当前会员的交易消息
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function loadMyMsg()
	{
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',Yii::app()->user->userid);
		$criteria->compare('zom_status',array(1,2,4));
		$criteria->order = " inputtime DESC ";

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	/*
	 * 返回状态
	* */
	public function getStatus($type=null){
		if($type==null){//1未审核2已审核3未查看4已查看,5删除
			return array(
				'1'=>'未查看',
				'2'=>'已查看',
			);
		}else{
			$level = $this->getStatus();
			return $level[$type];
		}
	}
	/*
	 * 提醒内容
	 * '消息类型:1下单,2付款,3发货,4,确认,5评价,6申请退款,7同意退款',
	* */
	public function getType($type=null,$inputtime=null){
			$inputtime = date('Y-m-d h:m',$inputtime);
			$contentArray = array(
				'1'=>"买家已于".$inputtime."下单",
				'2'=>"买家已于".$inputtime."付款",
				'3'=>"卖家已于".$inputtime."发货",
				'4'=>"买家已于".$inputtime."确认收货",
				'5'=>"买家已于".$inputtime."评价",
				'6'=>"买家已于".$inputtime."申请退款",
				'7'=>"卖家已于".$inputtime."成功退款",
				'8'=>"卖家已于".$inputtime."修改商品价格或运费",
			);
			return $contentArray[$type];
	}
	/*
	 * 发送交易提醒
	 * */
	public function setOrdMsg($zm_id,$order_id,$type){
		$this->__construct('insert');
		$this->zo_id = $order_id;
		$this->zom_type = $type;
		$this->zm_id = $zm_id;
		$this->zom_status = '1';
		$this->inputtime = time();
		$this->save(array('zo_id','zom_type','zm_id','zom_status','inputtime'));
	}
	
	/**
	 * 买家删除信息
	 * $idbox id数组
	 * $memberid 会员id
	 * */
	public function deleteMemMsg($idbox, $memberid=null){
		$condition = 'zom_id in('.implode(',', $idbox).')';
		if($memberid){
			$condition .= ' and zm_id='.$memberid;
		}
		$messages = $this->findAll($condition);
		foreach($messages as $message){
			$message->zom_status = $message->zom_status==4||$message->zom_status==5 ? 5 : 3;
			$message->save();
		}
		return true;
	}
}